using System;
using System.Collections.Generic;
using System.Text;
using Omniture.Studio.EventArgs;
using System.Windows.Forms;
using System.Drawing;

namespace Omniture.Studio.UserControls
{
	public class ColoredListBox : System.Windows.Forms.ListBox
	{
		public ColoredListBox()
			: base()
		{
			this.DrawMode = DrawMode.OwnerDrawFixed;
			this.DrawItem += new DrawItemEventHandler(listBox_DrawItem);
		}

		private void listBox_DrawItem(object sender, DrawItemEventArgs e)
		{
			if (e.Index == -1) return;			
			DrawingItemEventArgs eventParam = new DrawingItemEventArgs(this.Items[e.Index]);
			this.OnDrawingItem(eventParam);

			e.DrawBackground();
			Brush textBrush = new SolidBrush(e.ForeColor);
			Font drawFont = e.Font;

			if ((e.State & DrawItemState.Selected) > 0)
			{
				textBrush = SystemBrushes.HighlightText;			
			}
			else if (eventParam.Hilite)
			{
				textBrush = new SolidBrush(Color.Blue);
			}
			e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), drawFont, textBrush, e.Bounds);
		}

		#region Events

		public event EventHandler<DrawingItemEventArgs> DrawingItem;

		protected virtual void OnDrawingItem(DrawingItemEventArgs e)
		{
			if (this.DrawingItem != null)
			{
				this.DrawingItem(this, e);
			}
		}

		#endregion

	}
}
